# ======================================================================
#
# Copyright (C) ADN
# ADN::TMP::TMP TMP.pm
# Description:
#  - Gestion fichier temporaire
#  - Temporary file management
#
# ======================================================================

package ADN::TMP::TMP;

use strict;
use File::Temp;
use ADN::CONF::Read;

# #######################
# CREATE OBJECT
# #######################
sub new
{
	# Construction de l'objet $self
	my $self = shift;
	my $class = ref($self) || $self;
	return $self if ref $self;
	$self = bless {} => $class;
  $self->_init();
	return $self;
}

# #######################
# Fonction initialisation
# #######################
sub _init
{
	my $self = shift;
  # Chargement du fichier de configuration XML + Initialisation variable.
  my $conf = ADN::CONF::Read->new();
  ($conf =~ m/^[-]1$/smx) ? ($self->{error} = 1) : ();
  if ($self->{error} != 1)
  {
    my %TMP = $conf->TMP();
    ($conf->TMP() =~ m/^[-]1$/smx) ? ($self->{error} = 1) : ();

    $self->{path} = $TMP{path};
	  $self->{prefix} = $TMP{prefix};
	  $self->{suffix} = $TMP{suffix};
    $self->{path_inc} = $conf->_send_path();
	  undef $conf;
  }
  return;
}

# #######################
# START
# #######################
sub start
{
  my $self = shift;

  $self->{tmp} = File::Temp->new( TEMPLATE => $self->{prefix}.'_XXXX',
                                  DIR => $self->{path_inc}.$self->{path},
                                  SUFFIX => $self->{suffix},
                                  UNLINK => 1 );

  $self->{path} = $self->{tmp}->filename;
  $self->{file} = $self->file();
  $self->{path} =~ s/$self->{file}$//smx;
  return ($self->{path}, $self->file());
}

# #######################
# FILE TEMP
# #######################
sub file
{
  my $self = shift;
  my $file = $self->{tmp}->filename;
  $file =~ s/^.*[\\|\/]([^\\|^\/]+)$/$1/smx;
  return $file;
}

# #######################
# DESTROY OBJECT
# #######################
sub DESTROY {
    my $self = shift;
    return;
}
1;
__END__